/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.ShoppingCart.Logic;

import Deal.PersonalInformation.Logic.BasicInformation;
import Deal.PersonalInformation.Logic.CouponLogic;
import Deal.PersonalInformation.Logic.DiscountCouponLogic;
import Deal.PersonalInformation.Logic.EquivalentCouponLogic;
import Deal.PersonalInformation.Logic.OrderLogic;
import java.util.List;

/**
 *
 * @author 伯乐
 */
public class SettleAccountLogic {
    
    private BasicInformation basicInfo = new BasicInformation();
    private OrderLogic orderLogic = new OrderLogic();
    private CouponLogic couponLogic;
    
    public boolean dealDone(String type, String userID, int point, double account,
            List<String> couponList, List<Integer> numList, List<String[]> bookList, 
            String name, String phone, String postcode, String address){
        if( bookList == null ){ return false; }
        switch(type){
            case "打折券":
                couponLogic = new DiscountCouponLogic();
                point = 0;
            case "等价券":
                couponLogic = new EquivalentCouponLogic();
            default:
                couponList = null;    
        }
        
        boolean v_point = basicInfo.addToPoint(-point, userID);//减去已使用积分
        v_point &= basicInfo.addToPoint(calPoint(account), userID);//增加获得的积分
        boolean v_coupon = true;
        if(couponList != null){
           v_coupon = couponLogic.removeCoupon(userID, couponList, numList);//减去使用的优惠券
        }
        boolean v_order = orderLogic.addNewOrder(userID, account, bookList, name, phone, postcode, address);//生成订单
        
        return v_point && v_order && v_coupon;
        
    }
    
    private int calPoint(double account){ //由交易额计算可以获得的积分
        String s = String.valueOf(account);
        int loc;
        if((loc = s.indexOf(".")) != -1){
            s = s.substring(0, loc);
        }
        return Integer.parseInt(s);
    }
    
}
